<?php namespace Phpcmf\Controllers;

class Show extends \Phpcmf\Home\Module
{

    public function index() {
        $this->_module_init();
        $this->_Show(
            (int)\Phpcmf\Service::L('Input')->get('id'),
            [
                'field' => dr_safe_replace(\Phpcmf\Service::L('Input')->get('field')),
                'value' => dr_safe_replace(\Phpcmf\Service::L('Input')->get('value')),
            ],
            max(1, (int)\Phpcmf\Service::L('Input')->get('page'))
        );
    }

	

    public function time() {
        $this->_module_init();
        $this->_MyShow(
            'time',
            (int)\Phpcmf\Service::L('Input')->get('id'),
            max(1, (int)\Phpcmf\Service::L('Input')->get('page'))
        );
    }

    public function recycle() {
        $this->_module_init();
        $this->_MyShow(
            'recycle',
            (int)\Phpcmf\Service::L('Input')->get('id'),
            max(1, (int)\Phpcmf\Service::L('Input')->get('page'))
        );
    }

    public function draft() {
        $this->_module_init();
        $this->_MyShow(
            'draft',
            (int)\Phpcmf\Service::L('Input')->get('id'),
            max(1, (int)\Phpcmf\Service::L('Input')->get('page'))
        );
    }

    public function verify() {
        $this->_module_init();
        $this->_MyShow(
            'verify',
            (int)\Phpcmf\Service::L('Input')->get('id'),
            max(1, (int)\Phpcmf\Service::L('Input')->get('page'))
        );
    }
    /*
    *判断是否加入圈子
    */
    public function jiaruquanzi() {
        $cid = (int)\Phpcmf\Service::L('Input')->get('cid');
        $rt = \Phpcmf\Service::M()->table('1_quan_cart')->where('uid', $this->uid)->where('cid', $cid)->getRow();
        
        $fufei = \Phpcmf\Service::M()->table('1_quan_buy')->where('uid', $this->uid)->where('cid', $cid)->getRow();
        //判断付费
        $fufeidata = \Phpcmf\Service::M()->db->table('1_quan')->where('id', $cid)->get()->getRowArray();
        if(!$rt){
            if($fufei){
                if($fufei['endtime'] > time()){
                    $data['jiaru'] = '3';
                    $msg = '已经加入';
                    $code = 1;
                    return $this->_json($code, $msg,$data);
                } else {
                    $data['jiaru'] = '4';
                    $msg = '已经到期';
                    $code = 1;
                    return $this->_json($code, $msg,$data);
                }
            }
        }
        if($rt){
            
            $data['jiaru'] = '1';
            $msg = '';
            $code = 1;
            
        } elseif ($fufeidata['price'] > 0) {
            $data['jiaru'] = '2';
            $data['price'] = $fufeidata['price'];
            $msg = '付费后加入圈子';
            $code = 1;
        } else {
            $data['jiaru'] = '0';
            $msg = '';
            $code = 1;
        }
        $this->_json($code, $msg,$data);
    }

    /**
     * 加入圈子
     */
    public function cart() {

        !$this->uid && $this->_json(0, dr_lang('还没有登录'));

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
        !$id && $this->_json(0, dr_lang('id参数不完整'));

        $this->tablename = SITE_ID.'_'.APP_DIR;
        $data = \Phpcmf\Service::M()->db->table($this->tablename.'_index')->where('id', $id)->countAllResults();
        !$data && $this->_json(0, dr_lang('模块内容不存在'));

        $cart = \Phpcmf\Service::M()->db->table($this->tablename.'_cart')->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
        if ($cart) {
            // 已经退出了,我们就删除它
            \Phpcmf\Service::M()->db->table($this->tablename.'_cart')->where('id', intval($cart['id']))->delete();
            $msg = dr_lang('退出圈子');
            $c = 0;
        } else {
            //判断付费
            $fufeidata = \Phpcmf\Service::M()->db->table($this->tablename)->where('id', $id)->get()->getRowArray();
            $fufei = \Phpcmf\Service::M()->db->table($this->tablename.'_buy')->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
            if($fufei){
            	\Phpcmf\Service::M()->db->table($this->tablename.'_cart')->insert(array(
            	    'cid' => $id,
            	    'uid' => $this->uid,
            	    'read_name' => '',
            	    'read_url' => '',
            		'inputtime' => SYS_TIME,
            	));
            	$msg = dr_lang('加入圈子');
            	$c = 1;
            } else {
            	if ($fufeidata['price'] > 0) {
            		$msg = dr_lang('请先付费后加入圈子');
            		$c = 2;
            		$this->_json(2, $msg, $c);
            	} else {
            		\Phpcmf\Service::M()->db->table($this->tablename.'_cart')->insert(array(
            		    'cid' => $id,
            		    'uid' => $this->uid,
            		    'read_name' => '',
            		    'read_url' => '',
            			'inputtime' => SYS_TIME,
            		));
            		$msg = dr_lang('加入圈子');
            		$c = 1;
            	}
            }
        }
		// 更新数量
		$c = \Phpcmf\Service::M()->db->table($this->tablename.'_cart')->where('cid', $id)->countAllResults();
		\Phpcmf\Service::M()->db->table($this->tablename.'')->where('id', $id)->set('joins', $c)->update();
		\Phpcmf\Service::M('cache')->update_data_cache();
        // 返回结果
        $this->_json(1, $msg, $c);
    }
	/**
	 * 点赞圈子
	 */
	public function quanzan() {
	
	    !$this->uid && $this->_json(0, dr_lang('还没有登录'));
	
	    $id = (int)\Phpcmf\Service::L('Input')->get('id');
	    !$id && $this->_json(0, dr_lang('id参数不完整'));
	
	    $this->tablename = SITE_ID.'_'.APP_DIR;
	    $data = \Phpcmf\Service::M()->db->table($this->tablename.'_index')->where('id', $id)->countAllResults();
	    !$data && $this->_json(0, dr_lang('模块内容不存在'));
	
	    $zan = \Phpcmf\Service::M()->db->table($this->tablename.'_zan')->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
	    if ($zan) {
	        // 已经取消了,我们就删除它
	        \Phpcmf\Service::M()->db->table($this->tablename.'_zan')->where('id', intval($zan['id']))->delete();
	        $msg = dr_lang('取消点赞');
	        $c = 0;
	    } else {
	        \Phpcmf\Service::M()->db->table($this->tablename.'_zan')->insert(array(
	            'cid' => $id,
	            'uid' => $this->uid,
	            'read_name' => '',
	            'read_url' => '',
				'inputtime' => SYS_TIME,
	        ));
	        $msg = dr_lang('点赞圈子');
	        $c = 1;
	    }
		// 更新数量
		$c = \Phpcmf\Service::M()->db->table($this->tablename.'_zan')->where('cid', $id)->countAllResults();
		\Phpcmf\Service::M()->db->table($this->tablename.'')->where('id', $id)->set('quanzan', $c)->update();
		\Phpcmf\Service::M('cache')->update_data_cache();
	    // 返回结果
	    $this->_json(1, $msg, $c);
	}

    public function add_cart() {

        !$this->uid && $this->_json(0, dr_lang('还没有登录'));

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
        !$id && $this->_json(0, dr_lang('id参数不完整'));

        $sid = (int)\Phpcmf\Service::L('Input')->get('sid');
        !$sid && $this->_json(0, dr_lang('sid参数不完整'));

        $this->tablename = SITE_ID.'_'.APP_DIR;
        $data = \Phpcmf\Service::M()->table($this->tablename.'_form_son')->where('id', $sid)->getRow();
        !$data && $this->_json(0, dr_lang('圈贴内容不存在'));

        $cart = \Phpcmf\Service::M()->db->table($this->tablename.'_cart')->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
        if ($cart) {
            \Phpcmf\Service::M()->table($this->tablename.'_cart')->update($cart['id'], array(
                'read_name' => $data['title'],
                'read_url' => dr_son_url($data),
            ));
            $this->_json(1, 'ok');
        }

        $this->_json(0, dr_lang('未加入圈子'));
    }
}
